#include "sessionfriendarea.h"
#include <QScrollBar>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QStyleOption>
#include <QPainter>
#include "debug.h"
#include "model/data.h"
SessionFriendArea::SessionFriendArea(QWidget *parent)
    : QScrollArea{parent}
{
    //1.设置必要属性
    //设置这个属性才能开始滚动
    this->setWidgetResizable(true);
    //滚动条相关样式
    this->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width:4px;background-color:rgb(255, 255, 255);}");
    this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {height:0px;}");

    //设置item没有边框
    this->setStyleSheet("QScrollArea {border:none;}");
    //2.把widget创建出来
    container = new QWidget();
    container->setFixedWidth(310);
    this->setWidget(container);  //告诉滚动区域和哪个QWidget去配合

    //3.给这个widge指定布局管理器，以便后续添加元素进去
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    layout->setAlignment(Qt::AlignTop);
    container->setLayout(layout);

    // //测试一下
    // for(int i = 0;i<500;i++){
    //     QPushButton* btn = new QPushButton();
    //     btn->setText("按钮");
    //     btn->setStyleSheet("QPushButton{color: black;}");
    //     layout->addWidget(btn);
    // }


    //调试一下中间界面
#if TEST_UI
    QIcon icon(":/resource/image/defaultAvatar.jpg");

    for(int i = 0;i<30;++i){
        this->addItem(ApplyItemType, QString::number(i), icon, "张三"+QString::number(i), "最后一条消息"+QString::number(i));
    }
#endif

}

SessionFriendItem::SessionFriendItem(QWidget* owner, const QIcon& avatar, const QString& name, const QString& text)
    :owner(owner)
{
    this->setFixedHeight(70);  //高度
    this->setStyleSheet("QWidget {background-color:rgb(231, 231, 231);}");

    //创建网格布局管理区
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(20, 0, 0, 0);
    layout->setHorizontalSpacing(10);
    layout->setVerticalSpacing(0);
    this->setLayout(layout);   //添加到布局管理器到该页面

    //创建头像
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(QSize(50, 50));
    avatarBtn->setIconSize(QSize(50, 50));
    avatarBtn->setIcon(avatar);
    avatarBtn->setStyleSheet("QPushButton {border:none;}");
    //按钮尺寸的策略，Fixed固定尺寸，当前这个空间尺寸固定大小，不会随着窗口的尺寸变更产生变化
    //Expanding，尽可能伸展(把父元素的空间填满)
    avatarBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    //创建一个名字
    QLabel* nameLabel = new QLabel();
    nameLabel->setText(name);
    nameLabel->setStyleSheet("QLabel {font-size:18px; font-weight:600;}");
    nameLabel->setFixedHeight(35);
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);  //长度不固定，宽度固定

    //创建消息预览的 label
    messageLabel = new QLabel();
    messageLabel->setText(text);
    messageLabel->setFixedHeight(35);
    messageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);  //长度不固定，宽度固定

    //添加到布局管理器里面
    //头像处理0，0位置，占据两行两列
    layout->addWidget(avatarBtn,0, 0, 2, 2);
    layout->addWidget(nameLabel,0, 2, 1, 8);
    layout->addWidget(messageLabel, 1, 2, 1, 8);

}

void SessionFriendItem::paintEvent(QPaintEvent* event){
    (void)event;
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void SessionFriendItem::mousePressEvent(QMouseEvent* event){
    (void)event;
    select();
}
void SessionFriendItem::enterEvent(QEnterEvent* event){
    (void)event;
    //设置一个更深的颜色
    if(this->selected){
        return;
    }
    this->setStyleSheet("QWidget {background-color:rgb(222, 222, 222);}");
}
void SessionFriendItem::leaveEvent(QEvent* event){
    (void)event;
    //还原成一个原来的原色
    if(this->selected){
        return;
    }
    this->setStyleSheet("QWidget {background-color:rgb(231, 231, 231);}");
}

void SessionFriendItem::select(){
    //鼠标点击时会触发这个函数
    //点击时修改背景色
    //遍历其他原色，拿到所有其他兄弟元素
    const QObjectList children = this->parentWidget()->children();

    for(QObject* child:children){
        if(!child->isWidgetType()){
            //判定是否时widget
            continue;
        }
        //是widget
        SessionFriendItem* item = dynamic_cast<SessionFriendItem*>(child);
        if(item->selected){
            item->selected = false;
            item->setStyleSheet("QWidget {background-color:rgb(231, 231, 231);}");
        }
    }
    this->setStyleSheet("QWidget {background-color:rgb(210, 210, 210);}");
    //设置当前背景色，还原其他背景色
    this->selected = true;

    this->active();    //多态的语法
}

//清空该区域所有的item
void SessionFriendArea::clear(){
    QLayout* layout = container->layout();

    // //遍历并删除
    // for(int i = 0; i<layout->count();i++){
    //     layout->takeAt(i);   //一旦删除元素，上面的count就会改变，下标也会发生改变
    // }

    //遍历并删除
    for(int i = layout->count()-1; i>=0;i--){
        QLayoutItem* item = layout->takeAt(i);   //一旦移除元素，上面的count就会改变，下标也会发生改变

        //还要释放对象
        if(item->widget()){
            //把这个移除的widge元素进行释放
            delete item->widget();
        }
    }
}

//添加一个item之一，到底是会话申请还是申请还是好友申请，是子类
void SessionFriendArea::addItem(ItemType itemType,const QString& id,const QIcon& avatar, const QString& name, const QString& text){
    // SessionFriendItem* item = new  SessionFriendItem(this, avatar, name, text);
    SessionFriendItem* item = nullptr;
    if(itemType == SessionItemType){
        item = new SessionItem(this, id, avatar, name, text);
    }else if(itemType == FriendItemType){
        item = new FriendItem(this, id, avatar, name, text);
    }else if(itemType == ApplyItemType){
        item = new ApplyItem(this, id, avatar, name);
    }else{
        LOG()<<"错误的ItemType! itemType= "<<itemType;
        return;
    }
    container->layout()->addWidget(item);


}


//选中某个指定的item，通过index下标来进行下选择
void SessionFriendArea::clickItem(int index){
    if(index<0 || index>=container->layout()->count()){
        LOG()<<"点击元素的下标超出范围！index="<<index;
        return;
    }
    QLayoutItem* layoutItem = container->layout()->itemAt(index);
    if(layoutItem == nullptr || layoutItem->widget() == nullptr){
        LOG()<<"指定元素不存在！index="<<index;
        return;
    }

    //实际操作
    SessionFriendItem* item = dynamic_cast<SessionFriendItem*>(layoutItem->widget());
    item->select();
}


//父类的Item
void SessionFriendItem::active(){

}
//////////////////////////////////////////////
//会话Item的实现
////////////////////////////////////////////
SessionItem::SessionItem(QWidget* owner, const QString& chatSessionId, const QIcon& avatar,
                         const QString& name, const QString& lastMessage)
    :SessionFriendItem(owner, avatar, name, lastMessage), chatSessionId(chatSessionId)
{

}
void SessionItem::active(){
    //
    LOG()<<"点击 SessionItem 触发的逻辑! chatSessionId"<<chatSessionId;
}
//////////////////////////////////////////////
//好友Item的实现
////////////////////////////////////////////
FriendItem::FriendItem(QWidget* owner, const QString& userId, const QIcon& avatar,
                       const QString& name, const QString& description)
    :SessionFriendItem(owner, avatar, name, description), userId(userId)
{

}

void FriendItem::active(){
    LOG()<<"点击 FriendItem 触发的逻辑! userId"<<userId;
}
//////////////////////////////////////////////
//好友申请Item的实现
////////////////////////////////////////////
ApplyItem::ApplyItem(QWidget* owner, const QString& userId, const QIcon& avatar,
          const QString& name)
    :SessionFriendItem(owner, avatar, name, ""), userId(userId)
{
    //1.移除弗雷的messageLabel
    QGridLayout* layout = dynamic_cast<QGridLayout*>(this->layout());
    layout->removeWidget(messageLabel);
    //要记得释放内存
    delete messageLabel;

    //2. 创建两个按钮出来
    QPushButton* acceptBtn = new QPushButton();
    acceptBtn->setText("同意");
    QPushButton* rejectBtn = new QPushButton();
    rejectBtn->setText("拒绝");
    //3.添加到布局管理器中
    layout->addWidget(acceptBtn, 1, 2, 1, 1);
    layout->addWidget(rejectBtn, 1, 3, 1, 1);
}


void ApplyItem::active(){
    LOG()<<"点击 ApplyItem 触发的逻辑! chatSessionId"<<userId;
}
